package com.ytrsoft.base;

import com.ytrsoft.entity.Option;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JSelect extends JComboBox<Option> implements ActionListener {

    private SelectionChangeListener selectionChangeListener;

    public interface SelectionChangeListener {
        void onSelectionChanged(Option selectedOption);
    }

    public JSelect(Option[] options) {
        super(options);
        setUI(new ComboBoxUI<>(this));
        addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (selectionChangeListener != null) {
            selectionChangeListener.onSelectionChanged((Option) getSelectedItem());
        }
    }

    public void setSelectionChangeListener(SelectionChangeListener listener) {
        this.selectionChangeListener = listener;
    }
}
